package indi.zhifa.core.common.web.config.resttemplate;

import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

@Configuration
public class RestTemplateConfig {

    @Primary
    @Bean(name = "appRestClient")
    public RestTemplate getRestClient(ClientHttpRequestFactory pClientHttpRequestFactory, FastJsonHttpMessageConverter pFastJsonHttpMessageConverter) {

        RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder()
                .messageConverters(pFastJsonHttpMessageConverter)
                .setConnectTimeout(Duration.ofSeconds(2))
                .setReadTimeout(Duration.ofSeconds(5));
        RestTemplate restTemplate = restTemplateBuilder.build();
        restTemplate.setRequestFactory(pClientHttpRequestFactory);
        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SSL factory = new SSL(); //这里使用刚刚配置的SSL
        // SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//单位为ms
        factory.setConnectTimeout(5000);//单位为ms
        return factory;
    }

}
